package com.zhj.common.util;

import org.apache.commons.lang3.StringUtils;

public class StringUtil {

    /**
     * 匹配 *
     * @param basePackage
     * @param path
     * @return
     */
    public static boolean match(String basePackage, String path) {
        boolean anyBlank = StringUtils.isAnyBlank(basePackage, path);
        if (anyBlank) {
            return false;
        }
        int basePackageLength = basePackage.length();
        int basePackagePos = 0;

        int pathLength = path.length();
        int pathPos = 0;

        while (pathPos < pathLength && basePackagePos < basePackageLength) {
            char bc = basePackage.charAt(basePackagePos);
            char pc = path.charAt(pathPos);
            if (bc == '*' && pc != '.') {
                pathPos++;
                continue;
            }
            //将后面 . 跳过
            if (bc == '*') {
                basePackagePos++;
                bc = basePackage.charAt(basePackagePos);
            }
            if (bc != pc) {
                break;
            }
            pathPos++;
            basePackagePos++;
        }
        return basePackagePos == basePackageLength && pathPos == pathLength;
    }

}
